package com.kfd.controller;


import com.kfd.bean.BootstrapTableExpress;
import com.kfd.bean.Express;
import com.kfd.bean.Message;
import com.kfd.bean.ResultData;
import com.kfd.mvc.ResponseBody;
import com.kfd.service.ExpressService;
import com.kfd.util.JSONUtil;
import com.kfd.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController{


    @ResponseBody("/express/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        List<Map<String, Integer>> data = ExpressService.console();
        Message message = new Message();
        if(data.size() == 0){
            message.setStatus(-1);
        }else{
            message.setStatus(0);
        }
        message.setData(data);
        return JSONUtil.toJSON(message);
    }
    @ResponseBody("/express/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //获取查询数据的起始索引
        String offset = request.getParameter("offset");
        //获取要查询的数据量
        String pageNumber = request.getParameter("pageNumber");

        List<Express> all = ExpressService.findAll(true, Integer.valueOf(offset), Integer.valueOf(pageNumber));
        List<BootstrapTableExpress> list = new ArrayList<>();

        for (Express express : all) {
            BootstrapTableExpress bootstrapTableExpress = new BootstrapTableExpress(express.getId(),express.getNumber(),express.getUsername(),express.getUserphone(),express.getCompany(),(express.getCode() == null || express.getCode().equals(""))?"已取件":express.getCode(),String.valueOf(express.getIntime()),String.valueOf(express.getOuttime() == null?"未出库":express.getOuttime()),String.valueOf(express.getStatus() == 0?"待取件":"已取件"),express.getSysPhone());
            list.add(bootstrapTableExpress);
        }

        List<Map<String, Integer>> data = ExpressService.console();
        //bootstrap分页对象
        ResultData resultData = new ResultData();
        resultData.setRows(list);
        resultData.setTotal(data.get(0).get("data1_size"));
        //返回对象
        return JSONUtil.toJSON(resultData);
    }

    @ResponseBody("/express/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");

        boolean insert = ExpressService.insert(new Express(number, username, userPhone, company, UserUtil.getUserPhone(request.getSession())),0);

        Message message = new Message();

        if(insert){
            message.setStatus(0);
        }else{
            message.setStatus(-1);
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/find.do")
    public String find(HttpServletRequest request, HttpServletResponse response){
        String number = request.getParameter("number");
        Express byNumber = ExpressService.findByNumber(number);
        Message message = new Message();
        if(byNumber == null){
            message.setStatus(-1);
            message.setResult("单号不存在");
        }else{
            message.setStatus(0);
            message.setData(byNumber);
            message.setResult("查询成功");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        String number = request.getParameter("number");
        String company = request.getParameter("company");
        String username = request.getParameter("username");
        String userphone = request.getParameter("userphone");
        String status = request.getParameter("status");
        Express express = new Express();
        express.setId(Integer.valueOf(id));
        express.setNumber(number);
        express.setCompany(company);
        express.setUsername(username);
        express.setUserphone(userphone);
        express.setStatus(Integer.valueOf(status));
        express.setSysPhone(UserUtil.getUserPhone(request.getSession()));
        boolean update = ExpressService.update(express);

        Message message = new Message();
        if(update){
            message.setStatus(0);
            message.setResult("修改成功！");
        }else{
            message.setStatus(-1);
            message.setResult("修改失败！");
        }
        return JSONUtil.toJSON(message);
    }

    @ResponseBody("/express/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        boolean delete = ExpressService.delete(Integer.valueOf(id));
        Message message = new Message();
        if(delete){
            message.setStatus(0);
            message.setResult("删除成功！");
        }else{
            message.setStatus(-1);
            message.setResult("删除失败！");
        }
        return JSONUtil.toJSON(message);

    }

}
